const oneSecond = 1000
const oneMin = oneSecond * 60
const oneHour = oneMin * 60
const oneDay = oneHour * 24
const oneWeek = oneDay * 7

const formatCommentDate = (date) => {
  const dat = new Date(date)

  const now = new Date()

  // 获取时间戳计算距离发送时间
  const sub = now.getTime() - dat.getTime()
  if (sub <= oneMin) {
    return '刚刚'
  } else if (sub <= oneHour) {
    return parseInt(sub / oneMin) + '分钟前'
  } else if (sub <= oneDay) {
    return parseInt(sub / oneHour) + '小时前'
  } else if (sub <= oneWeek) {
    return parseInt(sub / oneDay) + '天前'
  }

  const month = dat.getMonth() + 1
  const day = dat.getDate()
  const year = dat.getFullYear()
  const nowYear = now.getFullYear()

  if (year != nowYear) {
    return year + '-' + month + '-' + day
  }

  return month + '-' + day
}

const formatTime = (time) => {
  const date = new Date(time)

  const year = date.getFullYear()
  let month = date.getMonth() + 1
  month = month < 10 ? '0' + month : month
  let day = date.getDate()
  day = day < 10 ? '0' + day : day
  let hour = date.getHours()
  hour = hour < 10 ? '0' + hour : hour
  let minute = date.getMinutes()
  minute = minute < 10 ? '0' + minute : minute
  let second = date.getSeconds()
  second = second < 10 ? '0' + second : second

  return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
}

export { formatCommentDate, formatTime }
